package com.uw.scenic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uw.scenic.entity.SysUser;
import com.uw.scenic.mapper.SysUserMapper;
import com.uw.scenic.service.TestServcie;
import com.uw.scenic.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class TestServcieImpl implements TestServcie {


    @Autowired
    private SysUserMapper userMapper;


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public SysUser findById(Long id) {
        return userMapper.selectById(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int save(UserDTO userDTO) {
        SysUser user=new SysUser();
        user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        return userMapper.insert(user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int update(UserDTO userDTO) {
        SysUser user=new SysUser();
        user.setId(userDTO.getId());
        user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        return userMapper.updateById(user);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int delete(Long id) {
        return userMapper.deleteById(id);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Page<SysUser> getPageList(Integer page, Integer pageSize) {
        Page<SysUser> pageInfo=new Page(page,pageSize);
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        pageInfo=userMapper.selectPage(pageInfo,queryWrapper);
        return  pageInfo;
    }
}
